package org.epidroid.epidroid;

import org.epidroid.epidroid.androsoul.engine.service.AndroSoulService;
import org.epidroid.epidroid.androsoul.ui.AndroSoulActivity;
import org.epidroid.epidroid.chronos.ui.ChronosActivity;
import org.epidroid.epidroid.epirss.RssDbAdaptater;
import org.epidroid.epidroid.epirss.RssView;
import org.epidroid.epidroid.news.ui.NewsActivity;
import org.epidroid.epidroid.ratp.ui.RatpUi;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class EpiDroid extends Activity {

	public static final Boolean IS_PROD = true;
	public static final int TODO_IMG = R.drawable.todo;
	public static RssDbAdaptater rssDb = null;
	private GridView _gridView = null;

	public static int[] _images = { R.drawable.wifi, R.drawable.rss_epita,
			R.drawable.androsoul, R.drawable.transport, R.drawable.newsgroups,
			R.drawable.chronos, TODO_IMG, TODO_IMG, R.drawable.about };
	
	public String[] _titles;
	public static Class<?>[] _activities = { ConfigureIitWifi.class,
			RssView.class, AndroSoulActivity.class, RatpUi.class,
			NewsActivity.class, ChronosActivity.class, null, null,
			AboutActivity.class };

	public static Boolean[] _isProd = { true, true, true, true, true, true,
			false, false, true };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		_gridView = (GridView) findViewById(R.id.main_gridview);
		_gridView.setAdapter(new ImageAdapter(this));

		bindService();
	}

	private void bindService() {
		Intent intent = new Intent(this, AndroSoulService.class);
		startService(intent);
	}

	public class ImageAdapter extends BaseAdapter {
		Context MyContext;

		public ImageAdapter(Context _MyContext) {
			MyContext = _MyContext;
		}

		@Override
		public int getCount() {
			/* Set the number of element we want on the grid */
			return _activities.length;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			_titles = getResources().getStringArray(R.array.menu_array);
			View MyView = convertView;

			if (convertView == null) {
				/* we define the view that will display on the grid */

				// Inflate the layout
				LayoutInflater li = getLayoutInflater();
				MyView = li.inflate(R.layout.menu_item, null);

				// Add The Text!!!
				TextView tv = (TextView) MyView
						.findViewById(R.id.grid_item_text);
				tv.setText(_titles[position]);

				// Add The Image!!!
				ImageView iv = (ImageView) MyView
						.findViewById(R.id.grid_item_image);

				if (!IS_PROD || _isProd[position]) {
					iv.setImageResource(_images[position]);
				} else {
					iv.setImageResource(TODO_IMG);
				}

				OnClickListener listener = new OnClickListener() {
					@Override
					public void onClick(View v) {
						if ((!IS_PROD || _isProd[position])
								&& (null != _activities[position])) {
							if (_activities[position].equals(ChronosActivity.class)) {
								// Start Epilife instead of our activity.
								// com.iiie.epilife/.views.SplashScreenActivity
								try {
									Intent epilife = new Intent(Intent.ACTION_MAIN);
									epilife.setComponent(ComponentName.unflattenFromString("com.iiie.epilife/com.iiie.epilife.views.SplashScreenActivity"));
									epilife.addCategory("android.intent.category.LAUNCHER");
									startActivity(epilife);
								}
								catch (android.content.ActivityNotFoundException e) {
									String appName = "com.iiie.epilife";
								    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appName)));
								}
							}
							else {
								startActivity(new Intent(v.getContext(),
										_activities[position]));
							}
						} else {
							EpiDroid.this
									.showToast(getString(R.string.available_soon));
						}
					}
				};
				MyView.setOnClickListener(listener);
			}

			return MyView;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}
	}

	/**
	 * Handy method for showing a toast
	 * 
	 * @param content
	 *            Message to be displayed in the toast
	 */
	public void showToast(String content) {
		Toast toast = Toast.makeText(getApplicationContext(), content,
				Toast.LENGTH_SHORT);
		toast.show();
	}

}
